<?php
use Workerman\Worker;
require_once __DIR__.'/Workerman/Autoloader.php';
$ws = new Worker("websocket://0.0.0.0:2345");
$ws->count = 1;

// worker进程启动后建立一个内部通讯端口
$ws->onWorkerStart = function($ws)
{
    // 开启一个内部端口，方便内部系统推送数据，Text协议格式 文本+换行符
    $inner_text_worker = new Worker('Text://0.0.0.0:5678');
    $inner_text_worker->onMessage = function($connection, $content)
    {
        global $ws;
        //拼装返回的数据结构
        $data = json_decode($content, true);
		$back_data = array('content' => $content['content'], 'nick' => $content['name'], 'type' => $content['type'], 'time' => date('Y-m-d H:i'));
       foreach ($ws->connections as $con)
        {
       		$con->send(json_encode($back_data));
           
        }
        $connection->send($content);
    };
    $inner_text_worker->listen();
};

//已连接客户端 
$ws->hasConnections = array();

$ws->onMessage = function ($connection, $message) use ($ws) {

	$data = json_decode($message,true);
	$content = '';
	switch ($data['type']) {
		case 'login':
			$ws->hasConnections[$connection->id] = array('name' => $data['name'], 'id' => $connection->id);
			$content = '欢迎 <i>'.$data['name'].'</i> 加入聊天室！    ';
			//拼装返回的数据结构
			$back_data = array('content' => $content, 'nick' => '<b style="color:red">系统：</b>', 'client_id' => $connection->id, 'client_name' => $data['name'], 'type' => 'login', 'clients' => $ws->hasConnections, 'time' => date('Y-m-d H:i'));
			
			break;
		case 'prisay':
			$recive_user = $ws->hasConnections[$data['to_client_id']]['name'];//接收者
			$send_user = $data['name'];//发送者
			$recive_content = '<i style="color:green">'. $send_user .'</i>对你 说：'.$data['content'];
			$send_content = '你对<i style="color:green">'. $recive_user .'</i> 说：'.$data['content'];
			//拼装返回的数据结构
			$back_data = array('content' => $recive_content, 'type' => 'say', 'nick' => $recive_user, 'time' => date('Y-m-d H:i'));
			$mycontent = array('content' => $send_content, 'type' => 'say', 'nick' => $send_user, 'time' => date('Y-m-d H:i'));
			break;
		default:
			//拼装返回的数据结构
			$back_data = array('content' => $data['content'], 'nick' => $data['name'], 'type' => 'say', 'time' => date('Y-m-d H:i'));
			
			break;
	}
	$mycontent = isset($mycontent)?$mycontent:'';
	//判断是否是私聊
	if ($data['to_client_id'] == 'all') {
		sendMessageToAll($back_data);
	} else {
		$connection->send(json_encode($mycontent));
		sendMessageToOne($data['to_client_id'], $mycontent);
	}
   	
	
};

$ws->onClose = function ($connection) use ($ws){
	$id = $connection->id;
	$name = $ws->hasConnections[$id]['name'];
	unset($ws->hasConnections[$id]);
	$message = array(
		'content' => $name.'悄悄的走了', 
		'nick' => '<b style="color:red">系统：</b>', 
		'type' => 'login', 
		'clients' => $ws->hasConnections, 
		'time' => date('Y-m-d H:i')
		);
	sendMessageToAll($message);
};

//向所有在线用户推送消息
function sendMessageToAll($message){
	global $ws;
	foreach ($ws->connections as $connection)
    {
   		$connection->send(json_encode($message));
       
    }
}

/**
** 向指定用户推送消息
** $cid $connection->id
** $message 消息 
**/
function sendMessageToOne($cid,$message){
	global $ws;
	$ws->connections[$cid]->send(json_encode($message));
}
//Worker::runAll();
?>